package com.wwwarehouse.common.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.wwwarehouse.common.R;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.adapter.filter.FilterSingleAdapter;
import com.wwwarehouse.common.bean.filter.FilterBean;
import com.wwwarehouse.common.eventbus_event.DrawerSingleSelectEvent;

import java.io.Serializable;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by Jackie on 2017/8/25.
 * 项目中的抽屉布局(单选)
 */

public class DeskDrawerSingleFragment extends BaseFragment {
    private View mRootView;
    private TextView mTitleTxt;
    private ListView mListView;

    private String mTitle;
    private List<FilterBean> mFilterList;
    private FilterSingleAdapter mAdapter;

    private static final String KEY_TITLE = "key_title";
    private static final String KEY_LIST = "key_list";

    public static final DeskDrawerSingleFragment newInstance(String title, List<FilterBean> filterList) {
        DeskDrawerSingleFragment deskDrawerSingleFragment = new DeskDrawerSingleFragment();

        Bundle bundle = new Bundle();
        bundle.putString(KEY_TITLE, title);
        bundle.putSerializable(KEY_LIST, (Serializable) filterList);

        deskDrawerSingleFragment.setArguments(bundle);

        return deskDrawerSingleFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_desk_drawer_single, container, false);
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initData();
        initEvent();
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    private void initView() {
        mTitleTxt = findView(mRootView, R.id.tv_title);
        mListView = findView(mRootView, R.id.lv_content);
    }

    private void initData() {
        Bundle bundle = getArguments();

        if (bundle != null) {
            mTitle = bundle.getString(KEY_TITLE);
            mFilterList = (List<FilterBean>) bundle.getSerializable(KEY_LIST);
        }

        mTitleTxt.setText(mTitle);

        mAdapter =  new FilterSingleAdapter(mActivity, mFilterList);
        mListView.setAdapter(mAdapter);
    }

    private void initEvent() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                FilterBean selectFilterBean = mFilterList.get(position);

                for (FilterBean filterBean : mFilterList) {
                    filterBean.setSelect(false);
                }

                selectFilterBean.setSelect(!selectFilterBean.isSelect());

                EventBus.getDefault().post(new DrawerSingleSelectEvent(position));

                mAdapter.notifyDataSetChanged();

                ((BaseCardDeskActivity) mActivity).hideDrawerLayout();
            }
        });
    }

    @Override
    public void setTitle() {

    }

    @Override
    public void requestDatas() {

    }
}
